<template>
  <div class="task">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>任务列表</span>
        </div>
      </template>

      <!-- 搜索表单 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="任务名称">
          <el-input v-model="searchForm.name" placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item label="处理人">
          <el-input v-model="searchForm.assignee" placeholder="请输入处理人" />
        </el-form-item>
        <el-form-item label="所属流程">
          <el-input v-model="searchForm.processName" placeholder="请输入流程名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>搜索
          </el-button>
          <el-button @click="resetSearch">
            <el-icon><Refresh /></el-icon>重置
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 任务类型标签切换 -->
      <div style="margin-bottom: 20px;">
        <el-radio-group v-model="taskType" @change="handleTaskTypeChange">
          <el-radio-button value="todo">待办任务</el-radio-button>
          <el-radio-button value="claim">可认领任务</el-radio-button>
          <el-radio-button value="done">已办任务</el-radio-button>
        </el-radio-group>
      </div>

      <!-- 数据表格 -->
      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="任务ID" width="180" />
        <el-table-column prop="name" label="任务名称" />
        <el-table-column prop="processName" label="所属流程" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="assignee" label="处理人" />
        <el-table-column prop="priority" label="优先级">
          <template #default="scope">
            <el-tag v-if="scope.row.priority >= 80" type="danger">高</el-tag>
            <el-tag v-else-if="scope.row.priority >= 50" type="warning">中</el-tag>
            <el-tag v-else type="info">低</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250">
          <template #default="scope">
            <div v-if="taskType === 'todo'">
              <el-button-group>
                <el-button type="primary" size="small" @click="handleComplete(scope.row)">
                  完成
                </el-button>
                <el-button type="warning" size="small" @click="handleTransfer(scope.row)">
                  转办
                </el-button>
                <el-button type="info" size="small" @click="handleViewDetail(scope.row)">
                  详情
                </el-button>
              </el-button-group>
            </div>
            <div v-else-if="taskType === 'claim'">
              <el-button-group>
                <el-button type="primary" size="small" @click="handleClaim(scope.row)">
                  认领
                </el-button>
                <el-button type="info" size="small" @click="handleViewDetail(scope.row)">
                  详情
                </el-button>
              </el-button-group>
            </div>
            <div v-else>
              <el-button type="info" size="small" @click="handleViewDetail(scope.row)">
                详情
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 任务详情对话框 -->
    <el-dialog v-model="detailDialogVisible" title="任务详情" width="800px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="任务ID">{{ currentTask.id }}</el-descriptions-item>
        <el-descriptions-item label="任务名称">{{ currentTask.name }}</el-descriptions-item>
        <el-descriptions-item label="所属流程">{{ currentTask.processName }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ currentTask.createTime }}</el-descriptions-item>
        <el-descriptions-item label="处理人">{{ currentTask.assignee }}</el-descriptions-item>
        <el-descriptions-item label="优先级">
          <el-tag v-if="currentTask.priority >= 80" type="danger">高</el-tag>
          <el-tag v-else-if="currentTask.priority >= 50" type="warning">中</el-tag>
          <el-tag v-else type="info">低</el-tag>
        </el-descriptions-item>
      </el-descriptions>

      <div class="task-form" v-if="taskType === 'todo'">
        <h3>任务表单</h3>
        <el-form label-width="100px">
          <el-form-item label="审批意见">
            <el-input type="textarea" rows="3" placeholder="请输入审批意见" />
          </el-form-item>
          <el-form-item label="审批结果">
            <el-radio-group v-model="approvalResult">
              <el-radio value="approve">同意</el-radio>
              <el-radio value="reject">拒绝</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>

      <div class="process-diagram" style="margin-top: 20px;">
        <div class="placeholder-diagram">流程图区域</div>
      </div>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="detailDialogVisible = false">关闭</el-button>
          <el-button v-if="taskType === 'todo'" type="primary" @click="handleCompleteInDialog">
            完成任务
          </el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 转办任务对话框 -->
    <el-dialog v-model="transferDialogVisible" title="转办任务" width="500px">
      <el-form label-width="100px">
        <el-form-item label="当前处理人">
          {{ currentTask.assignee }}
        </el-form-item>
        <el-form-item label="新处理人">
          <el-select v-model="newAssignee" placeholder="请选择新处理人">
            <el-option label="张三" value="zhangsan" />
            <el-option label="李四" value="lisi" />
            <el-option label="王五" value="wangwu" />
          </el-select>
        </el-form-item>
        <el-form-item label="转办说明">
          <el-input type="textarea" rows="3" v-model="transferReason" placeholder="请输入转办说明" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="transferDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmTransfer">确认转办</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { taskApi } from '@/api/flowable'

// 搜索表单数据
const searchForm = ref({
  name: '',
  assignee: '',
  processName: ''
})

// 表格数据
const tableData = ref([])
const loading = ref(false)

// 分页数据
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 当前任务类型
const taskType = ref('todo')

// 任务详情对话框
const detailDialogVisible = ref(false)
const currentTask = ref({})

// 转办任务对话框
const transferDialogVisible = ref(false)
const newAssignee = ref('')
const transferReason = ref('')

// 审批结果
const approvalResult = ref('approve')

// 获取任务列表
const getTaskList = async () => {
  loading.value = true
  try {
    const params = {
      ...searchForm.value,
      type: taskType.value,
      page: currentPage.value - 1,
      size: pageSize.value
    }
    const res = await taskApi.getList(params)
    tableData.value = res.data.data
    total.value = res.data.total
  } catch (error) {
    console.error('获取任务列表失败', error)
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  currentPage.value = 1
  getTaskList()
}

// 重置搜索
const resetSearch = () => {
  searchForm.value = {
    name: '',
    assignee: '',
    processName: ''
  }
  handleSearch()
}

// 分页大小改变
const handleSizeChange = (val) => {
  pageSize.value = val
  getTaskList()
}

// 当前页改变
const handleCurrentChange = (val) => {
  currentPage.value = val
  getTaskList()
}

// 任务类型切换
const handleTaskTypeChange = () => {
  currentPage.value = 1
  getTaskList()
}

// 查看详情
const handleViewDetail = (row) => {
  currentTask.value = row
  detailDialogVisible.value = true
}

// 完成任务
const handleComplete = (row) => {
  handleViewDetail(row)
}

// 在对话框中完成任务
const handleCompleteInDialog = async () => {
  try {
    await taskApi.complete(currentTask.value.id, {})
    ElMessage.success('任务完成成功')
    detailDialogVisible.value = false
    getTaskList()
  } catch (error) {
    ElMessage.error('任务完成失败')
  }
}

// 认领任务
const handleClaim = async (row) => {
  try {
    await taskApi.claim(row.id, 'zhangsan')
    ElMessage.success('任务认领成功')
    getTaskList()
  } catch (error) {
    ElMessage.error('任务认领失败')
  }
}

// 转办任务
const handleTransfer = (row) => {
  currentTask.value = row
  newAssignee.value = ''
  transferReason.value = ''
  transferDialogVisible.value = true
}

// 确认转办
const confirmTransfer = async () => {
  if (!newAssignee.value) {
    ElMessage.warning('请选择新处理人')
    return
  }
  
  try {
    await taskApi.transfer(currentTask.value.id, newAssignee.value)
    ElMessage.success('任务转办成功')
    transferDialogVisible.value = false
    getTaskList()
  } catch (error) {
    ElMessage.error('任务转办失败')
  }
}

onMounted(() => {
  getTaskList()
})
</script>

<style scoped>
.task {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.placeholder-diagram {
  height: 300px;
  border: 1px dashed #d9d9d9;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #909399;
}

.task-form {
  margin-top: 20px;
  border-top: 1px solid #ebeef5;
  padding-top: 20px;
}
</style> 